package cn.stylefeng.guns.sys.core.listener;

import cn.stylefeng.guns.sys.core.util.UUIDUtils;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyCrkDto;
import cn.stylefeng.guns.sys.modular.system.service.BtyCrkService;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;


@Slf4j
/*
 * 监听器批处理数据
 * */
public class BasicExcelListener extends AnalysisEventListener<BtyCrkDto> {


    private BtyCrkService crkService;

    public BasicExcelListener(){

    }

    public BasicExcelListener(BtyCrkService crkService){
        this.crkService = crkService;
    }


    /**
     * 批处理阈值2000
     */
    private static final int BATCH_COUNT = 74;
    List<BtyCrkDto> list = new ArrayList<BtyCrkDto>();

    @Override
    public void invoke(BtyCrkDto basic, AnalysisContext analysisContext) {
        basic.setId(UUIDUtils.getId());
        list.add(basic);
        if (list.size() >= BATCH_COUNT) {
            saveData();
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        analysisContext.readWorkbookHolder().setDefaultReturnMap(false);
        saveData();
        // log.info("所有数据解析完成！");

    }

    private void saveData(){
        //调用saveData()方法
        crkService.insertCrk(list);
    }

    }